package com.xiaolin.base.common;

import java.util.Arrays;

/**
 * @ClassName CountChar
 * @Description 输入一行字符，分别统计出其中英文字母、空格、数字和其它字符的个数。
 * @Detail detail
 * @Author MyPC
 * @Date 2020/8/9
 * @Version 1.0
 */
public class CountChar {

    public static void main(String[] args) {
        countChar("hhh  123**&你好");
    }

    public static void countChar(String string){
        //正则表达式
        String E1 = "[\u4e00-\u9fa5]";//汉字
        String E2 = "[a-zA-Z]";
        String E3 = "[0-9]";
        String E4 = "\\s";//空格
        int countChinese = 0;
        int countLetter = 0;
        int countNumber = 0;
        int countSpace = 0;
        int countOther = 0;
        //先将字符串转成字符数组
        char[] chars = string.toCharArray();
        //存放包含汉字的字符串数组
        String[] strings = new String[chars.length];
        for(int i=0;i<chars.length;i++){
            //汉字只能作为字符串处理
            strings[i]=String.valueOf(chars[i]);
        }
        for(int i=0;i<strings.length;i++){
            //使用正则表达式
            if(strings[i].matches(E1)){
                countChinese++;
            }else if(strings[i].matches(E2)){
                countLetter++;
            }else if(strings[i].matches(E3)){
                countNumber++;
            }else if(strings[i].matches(E4)){
                countSpace++;
            }else {
                countOther++;
            }
        }
        System.out.println("中文个数："+countChinese+"，英文个数："+countLetter+"，数字个数："+countNumber+"，空格个数："+countSpace+"，其他个数："+countOther);
    }
}
